package com.itmg.tools.page;

import javax.servlet.http.HttpServletRequest;
import com.itmg.util.Constants;

/**
 * 分页处理类，处理分页的逻辑
 * 
 * @author Jason
 */
public class PageHandler {

	/**
	 * 得到初始化的分页信息
	 * 
	 * @param request
	 * @return
	 */
	public static PageBean initPage(HttpServletRequest request) {
		int length = Constants.PART_PAGE_PER_COUNT;
		int offset = 0;
		int gotoPageIndex = 1;
		int total = 0;
		PageBean page = new PageBean();
		page.setOffset(offset);
		page.setLength(length);
		page.setGotoPage(gotoPageIndex);
		page.setTotal(total);
		return page;
	}

	/**
	 * 得到初始化的分页信息
	 * 
	 * @return
	 */
	public static PageBean initPage() {
		int length = Constants.PART_PAGE_PER_COUNT;
		int offset = 0;
		int gotoPageIndex = 1;
		int total = 0;
		PageBean page = new PageBean();
		page.setOffset(offset);
		page.setLength(length);
		page.setGotoPage(gotoPageIndex);
		page.setTotal(total);
		return page;
	}

	/**
	 * 根据传入的PageBean的total，offset，已经请求的动作，等信息 处理出完成此请求动作，正确的分页信息 请求的动作包括
	 * 
	 * 上一页，下一页，具体某页等
	 * 
	 * 
	 * 分页信息包括 数据总数，从那条数据开始取数，取多少条等
	 * 
	 * 
	 * @param page
	 * @throws Exception
	 */
	public static PageBean handle(PageBean page) throws Exception {
		int offset, curPage, begin, end, total, totalPage, gotoPage, length, startPage, endPage;
		offset = page.getOffset();
		curPage = page.getCurPage();
		begin = page.getBegin();
		end = page.getEnd();
		total = page.getTotal();
		totalPage = page.getTotalPage();
		gotoPage = page.getGotoPage();
		length = page.getLength();
		startPage = page.getStartPage();
		endPage = page.getEnd();

		if (gotoPage != 1) {
			offset = length * (gotoPage - 1);
		}
		begin = offset + 1;
		if (offset + length > total)
			end = total;
		else
			end = offset + length;

		curPage = offset / length + 1;
		if (total % length == 0) {
			totalPage = total / length;
		} else {
			totalPage = total / length + 1;
		}

		startPage = curPage - 5;
		if (startPage < 1)
			startPage = 1;
		endPage = startPage + 9;
		if (endPage > totalPage)
			endPage = totalPage;
		if (end == 0)
			begin = 0;

		page.setOffset(offset);
		page.setLength(length);
		page.setBegin(begin);
		page.setEnd(end);
		page.setCurPage(curPage);
		page.setEndPage(endPage);
		page.setGotoPage(gotoPage);
		page.setLength(length);
		page.setStartPage(startPage);
		page.setTotalPage(totalPage);
		page.setTotal(total);

		return page;
	}

	/**
	 * 从HttpServletRequest 参数中 得到分页请求的值，设置在PageSelect中
	 * 
	 * 
	 * @param request
	 * @return
	 */
	public static PageBean getPageRequest(HttpServletRequest request) {
		int length = Constants.PART_PAGE_PER_COUNT;
		PageBean page = (PageBean) request.getAttribute(Constants.PART_PAGE_KEY);
		if (page == null) {
			page = new PageBean();
			page.setOffset(0);
			page.setLength(length);
			page.setGotoPage(1);
		}
		return page;
	}

	/**
	 * 把分页信息 设置在 request 的Attribute中
	 * 
	 * @param request
	 * @param page
	 *            分页信息
	 * @throws Exception
	 */
	public static void putPage(HttpServletRequest request, PageBean page) throws Exception {
		try {
			request.setAttribute(Constants.PART_PAGE_KEY, page);
		} catch (Exception e) {
			return;
		}
	}
}
